<template>
  <div class="header">
    <div class="nav-topBar">
      <div class="container">
        <div class="topBar-menu">
          <a href="javascript:;">小米商城</a>
          <a href="javascript:;">MIUI</a>
          <a href="javascript:;">云服务</a>
          <a href="javascript:;">协议规则</a>
        </div>
        <div class="topBar-user">
          <a href="javascript:;" v-if="username">Hello,{{ username }}</a>
          <a href="javascript:;" v-if="!username" @click="login()">登录</a>
          <a href="javascript:;" v-if="username" @click="logout()">退出</a>
          <a
            href="javascript:;"
            v-if="username"
            @click="$router.push('order/list')"
            >我的订单</a
          >
          <a href="javascript:;" class="myCart" @click="goToCart()">
            <span class="icon-cart"></span> 购物车({{ count }})</a
          >
        </div>
      </div>
    </div>
    <div class="nav-header">
      <div class="container">
        <div class="header-logo">
          <a href="/#/index"></a>
        </div>
        <div class="header-menu">
          <div class="item-menu">
            <span>小米手机</span>
            <div class="children">
              <ul>
                <li class="product" v-for="item in phoneList" :key="item.id">
                  <a href="javascript:;" target="_blank">
                    <div class="goodsImg">
                      <img :src="item.mainImage" alt="" />
                    </div>
                    <p class="goodsName">{{ item.name }}</p>
                    <p class="goodsPrice">￥{{ item.price }}元</p>
                  </a>
                </li>
              </ul>
            </div>
          </div>
          <div class="item-menu">
            <span>Redmi 红米</span>
            <div class="children"></div>
          </div>
          <div class="item-menu">
            <span>电视</span>
            <div class="children"></div>
          </div>
        </div>
        <div class="header-search">
          <div class="wrapper">
            <input type="text" name="keyword" placeholder="Redmi 红米" />
            <a href="javascript:;" class="icon-search"></a>
          </div>
        </div>
      </div>
    </div>
    <!-- <div class="test">
      <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
      </ul>
    </div> -->
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex'
export default {
  name: 'nav-header',
  data() {
    return {
      // username: '',
      // username: this.$store.state.username,
      phoneList: [],
    }
  },
  computed: {
    ...mapState(['username', 'count']),
    // username() {
    //   return this.$store.state.username
    // },
    // cartCount() {
    //   return this.$store.state.count
    // },
  },
  mounted() {
    this.getProductList()
    if (this.$route.params.from == 'login') {
      console.log('from login')
      this.getCartCount()
    }
    // console.log(this.$store.state)
  },
  methods: {
    ...mapActions(['saveUserName', 'saveCartCount']),

    login() {
      this.$router.push('/login')
    },
    logout() {
      this.axios.post('/user/logout').then(() => {
        this.$message.info('退出成功')
        this.$cookie.set('userId', '', { exprires: '-1' })
        this.saveUserName('')
        this.saveCartCount()
      })
    },
    goToCart() {
      this.$router.push('/cart')
    },
    phoneList1() {
      return this.axios.get('/products', {
        params: {
          categoryId: '100012',
          pageSize: 6,
        },
      })
    },
    miList() {
      return this.axios.get('/products', {
        params: {
          categoryId: '100013',
          pageSize: 6,
        },
      })
    },
    getProductList() {
      this.axios
        .get('/products', {
          params: {
            categoryId: '100012',
            pageSize: 6,
          },
        })
        .then((res) => {
          // console.log(res)
          this.phoneList = res.list
        })

      // this.axios.all([this.phoneList1(), this.miList()]).then((res1, res2) => {
      //   console.log(res1, res2)
      // })
    },

    getCartCount() {
      this.axios.get('/carts/products/sum').then((res = 0) => {
        this.$store.dispatch('saveCartCount', res)
      })
    },
  },
}
</script>

<style lang="scss">
@import '../assets/scss/base.scss';
@import '../assets/scss/mixin.scss';
@import '../assets/scss/config.scss';

.header {
  .nav-topBar {
    height: 39px;
    line-height: 39px;
    background-color: #333;
    color: #b0b0b0;
    .container {
      @include flex();
      a {
        display: inline-block;
        color: #b0b0b0;
        margin-right: 17px;
      }
      .myCart {
        width: 110px;
        background-color: #ff6600;
        text-align: center;
        color: #fff;
        .icon-cart {
          display: inline-block;
          @include bgImg(16px, 12px, '/imgs/icon-cart-checked.png');
          margin-right: 4px;
        }
      }
    }
  }
  .nav-header {
    position: relative;
    .container {
      @include flex();
      height: 112px;

      .header-menu {
        display: inline-block;
        .item-menu {
          display: inline-block;
          font-size: 16px;
          font-weight: bold;
          color: #333;
          margin-right: 10px;
          // &:hover.children {
          //   display: block;
          // }
          &:hover {
            color: $colorA;
            .children {
              opacity: 1;
              height: 220px;
            }
          }
          span {
            cursor: pointer;
          }
          .children {
            opacity: 1;
            width: 100%;
            height: 0px;
            overflow: hidden;

            position: absolute;
            left: 0;
            top: 112px;

            border-top: 1px solid $colorH;
            box-shadow: 0px 7px 6px 0px rgba(0, 0, 0, 0.11);
            background-color: #fff;
            transition: all 0.1s;
            a {
              cursor: pointer;
            }
            ul {
              width: 1226px;
              margin: 0 auto;
              @include flex(space-around);
              .product + .product {
                position: relative;
                display: block;
                &::after {
                  content: '';
                  display: block;
                  width: 1px;
                  height: 80px;
                  background-color: $colorF;
                  position: absolute;
                  left: 0px;
                  top: 40px;
                }
              }
              .product {
                text-align: center;
                font-size: 12px;
                font-weight: bold;

                .goodsImg {
                  width: auto;
                  height: 110px;
                  margin-top: 26px;
                  img {
                    width: 100%;
                    height: 100%;
                  }
                }
                .goodsName {
                  color: $colorB;
                  margin-top: 19px;
                  margin-bottom: 8px;
                }
                .goodsPrice {
                  color: $colorA;
                }
              }
            }
          }
        }
      }
      .header-search {
        width: 309px;
        .wrapper {
          height: 50px;
          display: flex;
          border: 1px solid #e0e0e0;
          align-items: center;
          input {
            border: none;
            width: 245px;
            height: 50px;
            border-right: 1px solid #e0e0e0;
            padding-left: 14px;
          }
          .icon-search {
            display: inline-block;
            margin-left: 14px;
            @include bgImg(18px, 18px, '/imgs/icon-search.png'); // width: 52px;
          }
        }
      }
    }
  }
  .test {
    ul {
      li {
        display: inline-block;
        width: 20px;
      }
      li + li {
        border-right: 1px solid red;
      }
    }
  }
}
</style>
